package com.aaa.backsystem.controller;


import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.WxDangerService;
import com.aaa.backsystem.service.impl.FileUploadService;
import com.aaa.backsystem.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-08-22
 */
@Controller
@RequestMapping("/wxDanger")
@Api(tags = "隐患管理模块接口规范说明")
public class WxDangerController extends BaseController {

    @Autowired
    private WxDangerService wxDangerService;
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 隐患处理列表
     * @return
     */
    @ApiOperation(value = "查询所有隐患列表接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;用来查询所有隐患列表")
    @GetMapping("findDanger/{page}/{limit}")
    @ResponseBody
    public LayuiTable findDanger(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit){
        LayuiTable table = new LayuiTable();

        Wrapper<WxDanger> wrapper = new EntityWrapper();
        int count = wxDangerService.selectCount(wrapper);
        Page<WxDanger> pageInfo = new Page(page, limit);
        List<WxDanger> WxDangerList = wxDangerService.selectPage(pageInfo).getRecords();

        //从分页结果中提取list集合
        table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        table.setData(WxDangerList);
        table.setCount(count);
        return table;
    }

    /**
     * 处理隐患
     * @return
     */
    @ApiOperation(value = "处理隐患接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;对已处理的隐患点击标记为已经处理")
    @GetMapping("handleDanger/{dangerId}")
    @ResponseBody
    public Result handleDanger(@PathVariable("dangerId") Integer dangerId){
        try {
            WxDanger wxDanger = new WxDanger();
            wxDanger.setDangerId(dangerId);
            wxDanger.setStatus("1");
            wxDangerService.updateById(wxDanger);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }
    /**
     * 小程序隐患提交
     * @return
     */
    @ApiOperation(value = "隐患上报提交接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;巡检人员在小程序端进行上报隐患问题")
    @PostMapping("/addDanger")
    @ResponseBody
    public Result addProblemType(@RequestBody WxDanger wxDanger) {
        wxDanger.setStatus("0");
        boolean insert = wxDangerService.insertAllColumn(wxDanger);
        if (insert) {
            return success();
        } else {
            return error();
        }
    }
    /**
     * 小程序图片递交
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile uploadFile)
            throws Exception {
        FileUploadResult upload = this.fileUploadService.upload(uploadFile);
        String url = "https://"+upload.getName();
        return url;
    }
    /**
     * 小程序隐患管理界面显示
     * @return
     */
    @RequestMapping(value = "/toShowDanger",method = RequestMethod.GET)
    @ResponseBody
    public List toShowDanger(Model model) {
        Wrapper wrapper = new EntityWrapper();
        List problemTypeList = wxDangerService.selectList(wrapper);
        model.addAttribute("taskList", problemTypeList);
        return problemTypeList;
    }

}

